package com.woniu.controller;

import com.woniu.entity.Finances;
import com.woniu.service.FinancesService;
import com.woniu.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Gapper
 * @since 2021-03-29
 */
@RestController
public class FinancesController {

    @Autowired
    private FinancesService FinancesService;

    @PostMapping("finances/findAll/{value}")
    public ResultVO findAll(@PathVariable("value") String type){
        if(type.equals("全部") || type.equals("undefined")){
            type = "";
        }
        try{
            List<Map<String, String>> all = FinancesService.findAll(type);
          return ResultVO.success(all);
        }catch (Exception e){
            e.printStackTrace();
            return  ResultVO.failureForNull();
        }
    }

    @PostMapping("finances/save")
    public ResultVO save(@RequestBody Finances finances){
        boolean flag = false;
        try{
            flag = FinancesService.save(finances);
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag ? ResultVO.success() : ResultVO.failure();
    }

    @PostMapping("finances/delete")
    public ResultVO delete(@RequestBody String id){
        boolean flag = false;
        List<Integer> ids = new ArrayList<>();
        ids.add(Integer.valueOf(id));
        try{
            flag = FinancesService.delete(ids);
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag ? ResultVO.success() : ResultVO.failure();
    }

    @PostMapping("finances/updata")
    public ResultVO updata(@RequestBody Finances finances){
        boolean flag = false;
        try{
           flag = FinancesService.updata(finances);
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag ? ResultVO.success() : ResultVO.failure();
    }
}

